package com.fx.controller;

import com.alibaba.fastjson.JSONObject;
import com.fx.entity.GoodsInfo;
import com.fx.entity.UserCart;
import com.fx.service.GoodsInfoService;
import com.fx.service.UserCartService;
import com.fx.util.Constant;
import com.fx.util.PageInfo;
import com.fx.util.Query;
import com.fx.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yfl
 * @email 617823298@qq.com
 * @date 2018-06-19 16:25:21
 */

@Controller
@RequestMapping("/userCart")
public class UserCartController extends BaseController {
    @Autowired
    private UserCartService userCartService;
    @Autowired
    private GoodsInfoService goodsInfoService;
    private static String pageTitle = "购物车管理";
    private static String pageUrl = "/userCart";

    @GetMapping()
    String userCart(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.LIST_TITLE));
        return "userCart/userCart";
    }

    @ResponseBody
    @GetMapping("/list")
    public JSONObject list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<UserCart> userCartList = userCartService.getList(query);
        int total = userCartService.getCount(query);
        return renderPage(total, userCartList);
    }

    @GetMapping("/add")
    String add(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.ADD_TITLE));
        return "userCart/add";
    }

    @GetMapping("/edit/{flId}")
    String edit(@PathVariable("flId") String flId, Model model) {
        UserCart userCart = userCartService.getById(flId);
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.EDIT_TITLE));
        model.addAttribute("userCart", userCart);
        return "userCart/edit";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public R saveOrUpdate(UserCart userCart) {
        String id = userCart.getFlId();
        int result = 0;
        if (id == null || "".equals(id)) {
            result = userCartService.save(userCart);
        } else {
            result = userCartService.updateById(userCart);
        }
        if (result != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchDel")
    @ResponseBody
    public R remove(@RequestParam("ids") String[] ids) {
        if (userCartService.batchRemove(ids) != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    /**
     * 方法名称：获取用户购物车
     * 概要说明：根据用户id获取用户的购物车
     *
     * @Ather EDZ
     * @Date 2018-6-25 11:48
     * @Param userId 用户id
     * @Return
     */
    @GetMapping("/api/getUserCart")
    @ResponseBody
    public R getUserCart(HttpServletRequest request) {
        String userId = String.valueOf(request.getAttribute("flId"));
        Map<String, Object> result = new HashMap<String, Object>();
        List<UserCart> userCartList = userCartService.getCartByUserId(userId);
        result.put("userCartList", userCartList);
        double cartSubTotal = 0.00;
        for (UserCart cart : userCartList) {
            cartSubTotal += (cart.getFlPrice().doubleValue() * cart.getFlQuantity());
        }
        result.put("cartSubTotal", cartSubTotal);
        return R.ok(result);
    }

    /**
     * 方法名称：添加商品到购物车
     * 概要说明：添加商品到购物车
     *
     * @Ather EDZ
     * @Date 2018-6-25 12:00
     * @Param userId 登录用户id
     * @Param goodsId 要添加的商品id
     * @Param quantity 商品数量
     * @Return
     */
    @PostMapping("/api/addGoodsToCart")
    @ResponseBody
    public R addGoodsToCart(HttpServletRequest request,String goodsId,Integer quantity) {
        String userId = String.valueOf(request.getAttribute("flId"));
        GoodsInfo goodsInfo = goodsInfoService.getById(goodsId);
        UserCart cart = userCartService.addGoodsToCart(goodsInfo, userId, quantity);
        return R.ok(cart);
    }

    /**
     * 方法名称：从购物车移除商品
     * 概要说明：根据购物车id删除购物项
     * @Ather EDZ
     * @Date 2018-6-25 13:38
     * @Param
     * @Return
     */
    @PostMapping("/api/removeByCartId")
    @ResponseBody
    public R removeByCartId(String cartId) {
        return R.ok(userCartService.remove(cartId));
    }

    @PostMapping("/api/addQuantity")
    @ResponseBody
    public R removeByCartId(String cartId,Integer quantity) {
        return R.ok(userCartService.addQuantity(cartId,quantity));
    }


}
